<?
require_once(dirname(__FILE__).'/../models/Languages.php');
require_once(dirname(__FILE__).'/../models/LocaleSections.php');
require_once(dirname(__FILE__).'/../models/LocaleDefaults.php');
require_once(dirname(__FILE__).'/../models/LocaleTranslations.php');

class LanguagesController extends Main_Controller_Action {	
	public function preDispatch()
    {
        if (!unserialize(Zend_Auth::getInstance()->getIdentity())->getIsAdministrator()) {        
                $this->_helper->redirector('', '');
        } else {
           /**
			 * Translation - Loading dictionaries common and backoffice_common
			 */
			$commonLocale = Translator::loadTranslation($this->currentLang, 'common');
			$this->view->commonLocale = $commonLocale;
        }
    }
    
	function indexAction(){
		try{
				$languages = ModelMapper::instance('Languages')->findAll();
		} catch (DatabaseException $e){
				$e->getMessage();
		}
			
		$this->view->languages = $languages;
	}
	
	function addAction(){
		$smarty = $this->view->getEngine();

		$tpl = $smarty->fetch('languages/add.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function editAction(){
		$language = ModelMapper::instance('Languages')->find(intval($this->getRequest()->getParam('languagesId')));
		
		$smarty = $this->view->getEngine();
		
		$this->view->language = $language;
		$tpl = $smarty->fetch('languages/edit.tpl');
		
		$this->getResponse()->setHeader('Content-Type', 'text/html')->appendBody($tpl);
		$this->getResponse()->sendResponse();
		exit();
	}
	
	function saveAction(){
		$formData = $this->_request->getParam('addForm');
		
		if (intval($formData['languageId']) >0)
			$language = ModelMapper::instance('Languages')->find(intval($formData['languageId']));
		else
			$language = new Languages();
		
		$language->setLanguage(trim(strip_tags(stripcslashes($formData['language']))));
		$language->setShortcut(trim(strip_tags(stripcslashes($formData['shortcut']))));
		
		exit();
	}
	
	
}

	
?>
